package com.cy.io;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cy.common.ReadListener;
import com.cy.entity.ReadData;
import com.cy.entity.WriteData;
import com.cy.utils.DataUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: chenyun
 * @Date: 2021/09/08/13:59
 * @Description:
 */

// 读取数据类
public class Read {
    public static ReadData classroomName;
    private static ReadListener readListener = new ReadListener();

    public static List<ReadData> read(String path){
        String fileName = path;
        String week = null;
        LinkedList<ReadData> list = new LinkedList<ReadData>(); // 用来保持读取的数据
        List<WriteData> listHead = new ArrayList<WriteData>();// 用来将读的数据解析到写的数据
        EasyExcel.read(fileName)
                .head(ReadData.class)
                .sheet("Sheet0")
                .registerReadListener(readListener).doRead();
        list = readListener.getList();
        classroomName = DataUtils.getClassroomName(list.getFirst()); // 处理好的教室地点
        list.removeFirst();  // 这里移除了第一条数据  第一条数据是标题不需要处理
        list.removeLast();   // 这里移除了最后一条数据   不需要处理
        for (ReadData readData : list) {
            HashMap<String, List<String>> hashMap = new HashMap<>();

            // 这里是为了将null的星期填补
            if(readData.getWeek() != null){
                week = readData.getWeek();
            }else {
                readData.setWeek(week);
            }

            // 返回分割字符的索引下表  第一个教室里周数
            if(null != readData.getFirstClassroom()){
                List<Integer> index1 = DataUtils.searchAllIndex(readData.getFirstClassroom());
                List<String> string1 = DataUtils.allNum(index1, readData.getFirstClassroom().toString());
                hashMap.put(classroomName.getFirstClassroom(),string1);
            }

            if(null != readData.getSecondClassroom()){
                // 返回分割字符的索引下表  第二个教室里周数
                List<Integer> index2 = DataUtils.searchAllIndex(readData.getSecondClassroom().toString());
                List<String> string2 = DataUtils.allNum(index2, readData.getSecondClassroom().toString());
                hashMap.put(classroomName.getSecondClassroom(),string2);
            }

            if(null != readData.getThirdClassroom()){

                // 返回分割字符的索引下表  第三个教室里周数
                List<Integer> index3 = DataUtils.searchAllIndex(readData.getThirdClassroom().toString());
                List<String> string3 = DataUtils.allNum(index3, readData.getThirdClassroom().toString());
                hashMap.put(classroomName.getThirdClassroom(),string3);

            }

            if(null != readData.getFourthClassroom()){
                // 返回分割字符的索引下表  第四个教室里周数
                List<Integer> index4 = DataUtils.searchAllIndex(readData.getFourthClassroom().toString());
                List<String> string4 = DataUtils.allNum(index4, readData.getFourthClassroom().toString());
                hashMap.put(classroomName.getFourthClassroom(),string4);
            }

            if(null != readData.getFifthClassroom()){
                // 返回分割字符的索引下表  第五个教室里周数
                List<Integer> index5 = DataUtils.searchAllIndex(readData.getFifthClassroom().toString());
                List<String> string5 = DataUtils.allNum(index5, readData.getFifthClassroom().toString());
                hashMap.put(classroomName.getFifthClassroom(),string5);

            }
            readData.setClassNameAndListWeek(hashMap);
        }
        return list;
    }
    }




//    public static ReadData classroomName;
//
//
//
//    /**
//     * 读取数据
//     * @param path   excle表格的路径
//     * @return
//     */
//    public static List<ReadData> read(String path){
//
//        String fileName = path;
//        String week = null;
//        LinkedList<ReadData> list = new LinkedList<ReadData>(); // 用来保持读取的数据
//        List<WriteData> listHead = new ArrayList<WriteData>();// 用来将读的数据解析到写的数据
//        EasyExcel.read(fileName)
//                .head(ReadData.class)
//                .sheet("Sheet0")
//                .registerReadListener(new AnalysisEventListener<ReadData>() {
//                    // 每读取一条数据都会走这条语句
//                    @Override
//                    public void invoke(ReadData readData, AnalysisContext analysisContext) {
//                        list.add(readData);
//                    }
//                    // 数据完毕调用语句
//                    @Override
//                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//                        System.out.println("读取完毕");
//                    }
//                }).doRead();
//
//        classroomName = DataUtils.getClassroomName(list.getFirst()); // 处理好的教室地点
//        list.removeFirst();  // 这里移除了第一条数据  第一条数据是标题不需要处理
//        list.removeLast();   // 这里移除了最后一条数据   不需要处理
//        for (ReadData readData : list) {
//            HashMap<String, List<String>> hashMap = new HashMap<>();
//
//            // 这里是为了将null的星期填补
//            if(readData.getWeek() != null){
//                week = readData.getWeek();
//            }else {
//                readData.setWeek(week);
//            }
//
//            // 返回分割字符的索引下表  第一个教室里周数
//            if(null != readData.getFirstClassroom()){
//                List<Integer> index1 = DataUtils.searchAllIndex(readData.getFirstClassroom());
//                List<String> string1 = DataUtils.allNum(index1, readData.getFirstClassroom().toString());
//                hashMap.put(classroomName.getFirstClassroom(),string1);
//            }
//
//            if(null != readData.getSecondClassroom()){
//                // 返回分割字符的索引下表  第二个教室里周数
//                List<Integer> index2 = DataUtils.searchAllIndex(readData.getSecondClassroom().toString());
//                List<String> string2 = DataUtils.allNum(index2, readData.getSecondClassroom().toString());
//                hashMap.put(classroomName.getSecondClassroom(),string2);
//            }
//
//            if(null != readData.getThirdClassroom()){
//
//                // 返回分割字符的索引下表  第三个教室里周数
//                List<Integer> index3 = DataUtils.searchAllIndex(readData.getThirdClassroom().toString());
//                List<String> string3 = DataUtils.allNum(index3, readData.getThirdClassroom().toString());
//                hashMap.put(classroomName.getThirdClassroom(),string3);
//
//            }
//
//            if(null != readData.getFourthClassroom()){
//                // 返回分割字符的索引下表  第四个教室里周数
//                List<Integer> index4 = DataUtils.searchAllIndex(readData.getFourthClassroom().toString());
//                List<String> string4 = DataUtils.allNum(index4, readData.getFourthClassroom().toString());
//                hashMap.put(classroomName.getFourthClassroom(),string4);
//            }
//
//            if(null != readData.getFifthClassroom()){
//                // 返回分割字符的索引下表  第五个教室里周数
//                List<Integer> index5 = DataUtils.searchAllIndex(readData.getFifthClassroom().toString());
//                List<String> string5 = DataUtils.allNum(index5, readData.getFifthClassroom().toString());
//                hashMap.put(classroomName.getFifthClassroom(),string5);
//
//            }
//            readData.setClassNameAndListWeek(hashMap);
//        }
//        return list;
//    }


